package sketch;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 
 * @author Indrojeet Mondal
 * @version 1.0
 *
 */

class GraphController extends JPanel
{
	private GraphModel model = new GraphModel();
	private GraphView view;
	
	public GraphController(GraphView gview)
	{
		this.view = gview;
		
		this.setBorder(BorderFactory.createRaisedBevelBorder());
		Box box = Box.createVerticalBox();
		
		box.add(Box.createVerticalStrut(10));
		
		box.add(createEqnPanel());

		box.add(Box.createVerticalStrut(100));
		
		box.add(createSettingsPanel());
		
		add(box, BorderLayout.CENTER);
		
		updateFields();
	}
	
	
	private JPanel createEqnPanel()
	{
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(5, 2, 30, 5));
		
		panel.add(new JLabel("Equation Type: "));
		
		
		for (String s : GraphModel.typeStr)
			typeBox.addItem(s);
		typeBox.setEditable(false);
		
		typeBox.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e) 
				{
					model.eqnType = typeBox.getSelectedIndex();
					updateFields();
				}
			
			});
		panel.add(typeBox);
		
		panel.add(eqnLabel1);
		
		panel.add(eqnField1);
		panel.add(eqnLabel2);
		panel.add(eqnField2);
		
			
		ActionListener btnListener = new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				if (e.getActionCommand().equals("Sketch")) {
					model.equation1 = eqnField1.getText();
					model.equation2 = (model.eqnType == GraphModel.PARAMETRIC) ? eqnField2.getText() : null;
							
					model.xMin = Integer.parseInt(minField.getText().replace('+', ' ').trim());
					model.xMax = Integer.parseInt(maxField.getText().replace('+', ' ').trim());
				}
					
				else {
					model.xMin = -6;
					model.xMax = +6;
					model.curZoom = 1;
					
					minField.setText(String.format("%d", -6));
					maxField.setText(String.format("%d", 6));
					
				}
				view.updateView(model);
			}
		};
		
		sketchButton.addActionListener(btnListener);
		resetButton.addActionListener(btnListener);
		
		panel.add(sketchButton);
		panel.add(resetButton);
		
		ActionListener zoomListener = new ActionListener() {
			public void actionPerformed(ActionEvent  e)
			{
				if (e.getActionCommand().equals("ZoomIn"))
					view.zoomIn();
				else 
					view.zoomOut();
			}
		};
		zoomIn.addActionListener(zoomListener);
		zoomOut.addActionListener(zoomListener);
		
		panel.add(zoomIn);
		panel.add(zoomOut);
		
		
		return panel;
	}
	private JPanel createSettingsPanel()
	{
		JPanel setPanel = new JPanel();
		
		setPanel.setBorder(BorderFactory.createTitledBorder("Plot Settings"));
		setPanel.setLayout(new GridLayout(4, 2, 10, 25));
		
		Box box1 = Box.createHorizontalBox();
		
		box1.add(new JLabel("Xmin:"));
		box1.add(Box.createHorizontalStrut(10));
		box1.add(minField);
		
		setPanel.add(box1);
		
		Box box2 = Box.createHorizontalBox();

		box2.add(new JLabel("Xmax:"));
		box2.add(Box.createHorizontalStrut(10));
		box2.add(maxField);
		setPanel.add(box2);
		/*
		ActionListener checkListener = new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				settings.showAxes = axisBox.isSelected();
				settings.showGrid = gridBox.isSelected();
				GraphEditor.this.view.update(settings);
			}
		};
		axisBox.setSelected(settings.showAxes);
		axisBox.addActionListener(checkListener);
		setPanel.add(axisBox);
		
		gridBox.setSelected(settings.showGrid);
		gridBox.addActionListener(checkListener);
		setPanel.add(gridBox);*/
		
		ActionListener cbtnListener = new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				ColorButton source = (ColorButton)(e.getSource());
				Color color = source.getChosenColor();
				if (color == null)
					return;
				if (source == curveColor) 
					model.curveColor = color;
				else if (source == axesColor) 
					model.axesColor = color;/*
				else if (source == gridColor) 
					settings.gridColor = color;*/
				else
					model.backColor = color;
				view.updateView(model);
			}
		};
		
		setPanel.add(new JLabel("Background Color:"));
		backColor.addActionListener(cbtnListener);
		setPanel.add(backColor);
		
		setPanel.add(new JLabel("Axes Color:"));
		axesColor.addActionListener(cbtnListener);
		setPanel.add(axesColor);
		
		setPanel.add(new JLabel("Curve Color:"));
		curveColor.addActionListener(cbtnListener);
		setPanel.add(curveColor);
		
		/*
		setPanel.add(new JLabel("Grid Color:"));
		gridColor.addActionListener(cbtnListener);
		setPanel.add(gridColor);*/
		
		
		
		return setPanel;
	}
	private void updateFields()
	{
		typeBox.setSelectedIndex(model.eqnType);
		
		switch (model.eqnType) {
		case GraphModel.CARTESIAN:
			eqnLabel1.setText("f(x) = ");
			eqnField1.setText(model.equation1);
			eqnLabel2.setEnabled(false);
			eqnField2.setEnabled(false);
			break;
		case GraphModel.POLAR:
			eqnLabel1.setText("f(t) = ");
			eqnField1.setText(model.equation1);
			eqnLabel2.setEnabled(false);
			eqnField2.setEnabled(false);
			break;
		case GraphModel.PARAMETRIC:
			eqnLabel1.setText("f(t) = ");
			eqnField1.setText(model.equation1);
			eqnLabel2.setEnabled(true);
			eqnLabel2.setText("g(t) = ");
			eqnField2.setEnabled(true);
			eqnField2.setText(model.equation2);
			break;
		}
		minField.setText(String.valueOf(model.xMin));
		maxField.setText(String.valueOf(model.xMax));
	}
	
	private JComboBox typeBox = new JComboBox();
	
	private JLabel eqnLabel1 = new JLabel("f(x) = ");
	private JTextField eqnField1 = new JTextField();
	
	private JLabel eqnLabel2 = new JLabel("g(t) = ");
	private JTextField eqnField2 = new JTextField();
	
	private JButton sketchButton = new JButton("Sketch");
	private JButton resetButton = new JButton("Reset");
	
	private JButton zoomIn = new JButton("ZoomIn");
	private JButton zoomOut = new JButton("ZoomOut");
	
	private JTextField minField = new JTextField();
	private JTextField maxField = new JTextField();
	/*
	private JCheckBox axisBox = new JCheckBox("Show Axes");
	private JCheckBox gridBox = new JCheckBox("Show Grid");
	*/
	private ColorButton curveColor = new ColorButton(model.curveColor);
	private ColorButton axesColor = new ColorButton(model.axesColor);
	//private ColorButton gridColor = new ColorButton(settings.gridColor);
	private ColorButton backColor = new ColorButton(model.backColor);
}
